package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Appointment;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    @Transactional
    public void save(Appointment appointment) {
        //预约单流水号（年月日时分秒+5位随机数）
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        int random = new Random().nextInt(90000) + 10000;
        appointment.setAno(date+random);
        appointment.setCreateTime(new Date());
        //调mapper接口中的方法
        appointmentMapper.insert(appointment);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(Appointment appointment) {
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Appointment> listAll() {
        return appointmentMapper.selectAll();
    }

    @Override
    public PageInfo<Appointment> query(AppointmentQueryObject qo) {
        //定义查询页,每页页数,自动查询 并加limit参数
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"a.create_time desc");
        List<Appointment> list = appointmentMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void updateStatus(Long id, int status) {
        appointmentMapper.updateStatus(id,status);
    }

}
